****************************************************************************************************************
**																											  **
** This do file is part of the replication material for the following article: 								  **
**  "Is There an Ideological Asymmetry in the Incumbency Effect? Evidence from U.S. Congressional Elections." **
** 		Authors: Morisi, Davide, Jost, John, Panagopoulos, Costas, and Jussi Valtonen		    			  **
** 		Journal: Social Psychological and Personality Science																	  **
**																											  **
** This file replicates the following step of the analysis: 												  **
**																											  **
**		RECODING CCES 2018 ORIGINAL DATASET			   													  	  **
**																											  **
****************************************************************************************************************

*Set working directory
cd "C:\Users\Davide Morisi\Dropbox\datasets\CCES"

*Open original dataset
*Brian Schaffner; Stephen Ansolabehere; Sam Luks, 2019, "CCES Common Content, 2018", https://doi.org/10.7910/DVN/ZSBZ7K, Harvard Dataverse, V6, UNF:6:hFVU8vQ/SLTMUXPgmUw3JQ== [fileUNF] 
*Downloaded on January 16, 2020 from https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi%3A10.7910/DVN/ZSBZ7K
use "2018\cces18_common_vv.dta", clear


***
*covariates

*gender
fre gender // 2=female
*age
sum birthyr
*education
fre educ 
*race
fre race
recode race 4/8=99, gen(racer) // 99=other
*religion
fre religpew
recode religpew 3/8=3 12=3 9=4 10=5 11=6, gen(religpewr)
fre religpewr
*economy
fre CC18_301
rename CC18_301 economy
*state
fre inputstate
rename inputstate state
clonevar state18 = state
*weight
rename commonpostweight weight1


*********************
*partisanship / ideology

*party of respondent - profile
*3 categories
*1 democrat, 2 republican, 3 independent, 4 other, 5 dk
fre pid3
*7 categories
*1 strong dem, 2 not strong dem, 3 lean dem, 4 indep, 5 lean rep, 6 not strong rep, 7 strong rep, 8 dk 
fre pid7
ta pid7 pid3 // leaning partisans coded as independent/other/dk

*party id post
*3 categories (note: cat 5 dk missing)
fre CC18_421a // pid3
rename CC18_421a pid3post
*5 categories
fre CC18_421b // leaners
rename CC18_421b leaners_prof_18
ta pid3post if leaners_prof_18!=. // asked only for independents/others + 1 democrat
fre pid3post leaners_prof_18
gen pid5post = 1 if pid3post==1
replace pid5post = 2 if pid3post>2 & leaners_prof_18==1
replace pid5post = 3 if pid3post==3 & leaners_prof_18==3
replace pid5post = 4 if pid3post>2 & leaners_prof_18==2
replace pid5post = 5 if pid3post==2
*1 dem, 2 lean dem, 3 indep, 4 lean rep, 5 rep
fre pid5post

*ideology profile
*1 very lib, 2 lib, 3 moderate, 4 cons, 5 very cons, 6 dk
fre ideo5

*ideology from pre-election survey
*1 very lib, 2 lib, 3 somewhat lib, 4 mod, 5 somewhat con, 6 con, 7 very con, 8dk
fre CC18_334A
rename CC18_334A ideo7


*****
*vote post
*turnout
fre CC18_401
rename CC18_401 turnout18
clonevar turnout = turnout18
*senate
fre CC18_410b 
rename CC18_410b votesen18
clonevar vote_sen = votesen18
*house
fre CC18_412
rename CC18_412 votehou18
clonevar vote_hou = votehou18


****
*Create incumbent var - SENATE

*Party of candidates
des SenCand1Party_post SenCand2Party_post
fre SenCand1Party_post SenCand2Party_post
encode SenCand1Party_post, gen(SenCand1Party_postr)
encode SenCand2Party_post, gen(SenCand2Party_postr)
fre SenCand1Party_postr SenCand2Party_postr
recode SenCand1Party_postr 1=1 2=3
recode SenCand2Party_postr 1=1 2=2
fre SenCand1Party_post SenCand1Party_postr
fre SenCand2Party_post SenCand2Party_postr

*Candidate 1
fre SenCand1Name_post
encode SenCand1Name_post, gen(SenCand1Name_postr)
fre SenCand1Name_postr
*Candidate 2
fre SenCand2Name_post
encode SenCand2Name_post, gen(SenCand2Name_postr)
fre SenCand2Name_postr

*Create incumbent variables (see excel sheet for coding based on https://en.wikipedia.org/wiki/2018_United_States_Senate_elections)
fre SenCand1Name_postr if SenCand1Party_postr==1 & SenCand2Party_postr==2
fre SenCand2Name_postr if SenCand1Party_postr==1 & SenCand2Party_postr==2
fre SenCand1Name_postr SenCand2Name_postr if state==6
fre SenCand1Name_postr SenCand1Party_postr if state==6

gen SenCand1Incumbent_postr = 0 if SenCand1Name_postr!=.
replace SenCand1Incumbent_postr = 1 if SenCand1Name_postr==1	| SenCand1Name_postr==3	| SenCand1Name_postr==6	| SenCand1Name_postr==7	| SenCand1Name_postr==8	| SenCand1Name_postr==9	| SenCand1Name_postr==10	| SenCand1Name_postr==12	| SenCand1Name_postr==14	| SenCand1Name_postr==16	| SenCand1Name_postr==20	| SenCand1Name_postr==21	| SenCand1Name_postr==22	| SenCand1Name_postr==23	| SenCand1Name_postr==25	| SenCand1Name_postr==26	| SenCand1Name_postr==27	| SenCand1Name_postr==29	| SenCand1Name_postr==30	| SenCand1Name_postr==31	| SenCand1Name_postr==32	| SenCand1Name_postr==33
fre SenCand1Incumbent_postr

gen SenCand2Incumbent_postr = 0 if SenCand2Name_postr!=.
replace SenCand2Incumbent_postr = 1 if SenCand2Name_postr==9	| SenCand2Name_postr==32	| SenCand2Name_postr==4	| SenCand2Name_postr==3	| SenCand2Name_postr==29
fre SenCand2Incumbent_postr // fewer republican incumbents

ta SenCand1Incumbent_postr SenCand2Incumbent_postr

*states without incumbent
*no incumbent in arizona, tennessee & utah
*in calif: feinstein competing against another dem
*in maine & vermont: independent incumbent
ta state if SenCand1Incumbent_postr==0 & SenCand2Incumbent_postr==0


******
*contextual var SENATE

*incumbent candidates
label de incumb 0"not incumbent" 1"incumbent", modify
label values SenCand1Incumbent_postr incumb
label values SenCand2Incumbent_postr incumb
fre SenCand1Incumbent_postr SenCand2Incumbent_postr
ta SenCand1Incumbent_postr SenCand2Incumbent_postr

*party of candidate
label de partycand 1"dem. candidate" 2"rep. candidate" 3"other candidate", modify
label val SenCand1Party_postr partycand
label val SenCand2Party_postr partycand
fre SenCand1Party_postr SenCand2Party_postr

*republican vs dem incumbent
fre SenCand1Party_postr SenCand1Incumbent_postr
fre SenCand2Party_postr SenCand2Incumbent_postr
ta SenCand1Party_postr SenCand2Party_postr
gen inc_rep_sen = 0 if (SenCand1Party_postr==1 & SenCand2Party_postr==2) & (SenCand1Incumbent_postr==1 & SenCand2Incumbent_postr==0)
replace inc_rep_sen = 1 if (SenCand1Party_postr==1 & SenCand2Party_postr==2) & (SenCand1Incumbent_postr==0 & SenCand2Incumbent_postr==1)
fre inc_rep_sen
*including districts with no dem/rep incumbents
ta SenCand1Incumbent_postr SenCand2Incumbent_postr if (SenCand1Party_postr==1 & SenCand2Party_postr==2)
gen inc_all_sen = inc_rep_sen
recode inc_all_sen 0=-1
replace inc_all_sen = 0 if (SenCand1Party_postr==1 & SenCand2Party_postr==2) & (SenCand1Incumbent_postr==0 & SenCand2Incumbent_postr==0)
fre inc_all_sen // -1=dem inc, 0=inc not dem nor rep, 1=rep inc

*keep only states with elections, not open elections
ta state inc_rep_sen, nol
*filter
ta SenCand1Name_post
bys state: ta SenCand1Name_post if inc_rep_sen!=.
gen count=1 if inc_rep_sen!=.
egen count_sum = sum(count), by(state SenCand1Name_post)
gen filter_sen=1 if inc_rep_sen!=.
replace filter_sen=2 if count_sum<10 & inc_rep_sen!=.
ta state inc_rep_sen
ta state inc_all_sen
ta state inc_rep_sen if filter_sen==1
clonevar filter_sen18 = filter_sen


****
*Create incumbent var - HOUSE

*Party of candidates
des HouseCand1Party_post HouseCand2Party_post
fre HouseCand1Party_post HouseCand2Party_post
encode HouseCand1Party_post, gen(HouseCand1Party_postr)
encode HouseCand2Party_post, gen(HouseCand2Party_postr)
fre HouseCand1Party_postr HouseCand2Party_postr
recode HouseCand1Party_postr 1=1 2=2
recode HouseCand2Party_postr 1=1 2/6=3 7=2 8=3
fre HouseCand1Party_postr HouseCand2Party_postr

*Candidate 1
fre HouseCand1Name_post
encode HouseCand1Name_post, gen(HouseCand1Name_postr)
fre HouseCand1Name_postr
ta HouseCand1Name_postr
*Candidate 2
fre HouseCand2Name_post
encode HouseCand2Name_post, gen(HouseCand2Name_postr)
fre HouseCand2Name_postr
ta HouseCand2Name_postr

fre HouseCand1Name_postr HouseCand1Party_postr state  if HouseCand1Name_postr==394
fre HouseCand2Name_postr HouseCand2Party_postr state  if HouseCand2Name_postr==232

*merging with incumbent info from excel
gen cand1_num = HouseCand1Name_postr if HouseCand1Name_postr!=.
gen cand2_num = HouseCand2Name_postr if HouseCand2Name_postr!=.
tab1 cand1_num cand2_num
/*preserve
	import excel "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Recoding - merging\2018 House GE cands.xlsx", sheet("cand1_num") firstrow clear
	save "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Recoding - merging\2018_cand1_num.dta"
	import excel "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Recoding - merging\2018 House GE cands.xlsx", sheet("cand2_num") firstrow clear
	save "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Recoding - merging\2018_cand2_num.dta"
restore*/
merge m:1 cand1_num using "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect_davide\Recoding - merging\2018_cand1_num.dta"
drop _merge
merge m:1 cand2_num using "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect_davide\Recoding - merging\2018_cand2_num.dta"
drop _merge
*matching seems to work
ta HouseCand1Name_postr cand1_inc
ta HouseCand2Name_postr cand2_inc
sort state
list HouseCand1Name_postr cand1_inc state HouseCand2Name_postr cand2_inc in 3500/3520
list HouseCand1Name_postr cand1_inc state HouseCand2Name_postr cand2_inc in 9000/9030
list HouseCand1Name_postr cand1_inc state HouseCand2Name_postr cand2_inc in 30000/30030
list HouseCand1Name_postr cand1_inc state HouseCand2Name_postr cand2_inc in 40100/40130

*create incumbent variables
*cand1_inc > 0=challenger, 1=incumbent, 88=open seat, 90=independent, 99=didn't run
fre cand1_inc
gen HouseCand1Incumbent_postr = cand1_inc
recode HouseCand1Incumbent_postr 88=0 99=.
fre cand2_inc
gen HouseCand2Incumbent_postr = cand2_inc
recode HouseCand2Incumbent_postr 88/90=0


******
*contextual var HOUSE
label val HouseCand1Incumbent_postr incumb
label val HouseCand2Incumbent_postr incumb
fre HouseCand1Incumbent_postr HouseCand2Incumbent_postr
ta HouseCand1Incumbent_postr HouseCand2Incumbent_postr

*party of candidate
label val HouseCand1Party_postr partycand
label val HouseCand2Party_postr partycand
fre HouseCand1Party_postr HouseCand2Party_postr

*republican incumbent vs. dem incumbent
*note: with this var we restrict analysis to seats with dem vs. rep AND with inc. vs. not inc.
fre HouseCand1Party_postr HouseCand1Incumbent_postr
fre HouseCand2Party_postr HouseCand2Incumbent_postr
ta HouseCand1Party_postr HouseCand2Party_postr // no cases when candidate 1 is republican and candidate 2 is democrat
ta HouseCand1Incumbent_postr HouseCand2Incumbent_postr if (HouseCand1Party_postr==1 & HouseCand2Party_postr==2)
gen inc_rep_hou = 0 if (HouseCand1Party_postr==1 & HouseCand2Party_postr==2) ///
& (HouseCand1Incumbent_postr==1 & HouseCand2Incumbent_postr==0)
replace inc_rep_hou = 1 if (HouseCand1Party_postr==1 & HouseCand2Party_postr==2) ///
& (HouseCand1Incumbent_postr==0 & HouseCand2Incumbent_postr==1)
fre inc_rep_hou
*including districts with no dem/rep incumbents
ta HouseCand1Incumbent_postr HouseCand2Incumbent_postr if (HouseCand1Party_postr==1 & HouseCand2Party_postr==2) 
gen inc_all_hou = inc_rep_hou
recode inc_all_hou 0=-1
replace inc_all_hou = 0 if (HouseCand1Party_postr==1 & HouseCand2Party_postr==2) & (HouseCand1Incumbent_postr==0 & HouseCand2Incumbent_postr==0)
fre inc_all_hou

*keep only states with elections, not open elections
ta state inc_rep_hou
*filter
gen count1=1 if inc_rep_hou!=.
egen count_sum1 = sum(count1), by(state HouseCand1Name_post)
gen filter_hou=1 if inc_rep_hou!=.
replace filter_hou=2 if count_sum1<10 & inc_rep_hou!=.
ta state inc_rep_hou if filter_hou==1
*double check
bys state: ta HouseCand1Name_post if filter_hou==1 // e.g. north dakota missing because no incumbent
ta state if HouseCand1Name_post=="Mac Schneider" // seat without incumbent
ta state if HouseCand1Name_post=="Mac Schneider" & filter_hou==1 // removed when filter applied
clonevar filter_hou18 = filter_hou

*check north dakota=38
fre cdid115_post cdid116_post if state==38
list HouseCand1Name_postr cand1_inc state HouseCand2Name_postr cand2_inc if state==38 & cdid116_post=="1"
list HouseCand1Name_postr cand1_inc state HouseCand2Name_postr cand2_inc if state==38 & cdid116_post=="4"
list HouseCand1Name_postr cand1_inc state HouseCand2Name_postr cand2_inc if state==38 & cdid116_post=="7"
list HouseCand1Name_postr cand1_inc state HouseCand2Name_postr cand2_inc if state==38 & cdid116_post=="9"
fre state if HouseCand1Name_post=="Ryan Trundle"
fre state if HouseCand1Name_post=="Collin Peterson"
fre state if HouseCand1Name_post=="Liz Watson"
fre state if HouseCand2Name_post=="Trey Hollingsworth"


*******************
*combined variables > vote + incumbent status

*vote + incumbent status, all options - SENATE
ta vote_sen inc_all_sen
fre turnout vote_sen
fre inc_all_sen // -1=dem inc, 0=inc not dem nor rep, 1=rep inc
gen votinc_sen_all = 0 if turnout<5 | vote_sen==5 // did not vote
replace votinc_sen_all = 10 if vote_sen==1 & inc_all_sen==-1 & SenCand1Party_postr==1
replace votinc_sen_all = 11 if vote_sen==1 & inc_all_sen==0 & SenCand1Party_postr==1
replace votinc_sen_all = 12 if vote_sen==1 & inc_all_sen==1 & SenCand1Party_postr==1
replace votinc_sen_all = 20 if vote_sen==2 & inc_all_sen==-1 & SenCand2Party_postr==2
replace votinc_sen_all = 21 if vote_sen==2 & inc_all_sen==0 & SenCand2Party_postr==2
replace votinc_sen_all = 22 if vote_sen==2 & inc_all_sen==1 & SenCand2Party_postr==2
*label var votinc_sen_all "Voted for dem/rep by incumbent status - SENATE"
*label de votinc_sen_all 0"did not vote" 10"vote dem, dem inc"  11"vote dem, no inc"  12"vote dem, rep inc" ///
*20"vote rep, dem inc"  21"vote rep, no inc"  22"vote rep, rep inc" 
*label val votinc_sen_all votinc_sen_all
fre votinc_sen_all

*vote + incumbent status, all options - HOUSE
ta vote_hou inc_all_hou
fre vote_hou
fre inc_all_hou // -1=dem inc, 0=inc not dem nor rep, 1=rep inc
gen votinc_hou_all = 0 if turnout<5 | vote_hou==8  // did not vote
replace votinc_hou_all = 10 if vote_hou==1 & inc_all_hou==-1 & HouseCand1Party_postr==1
replace votinc_hou_all = 11 if vote_hou==1 & inc_all_hou==0 & HouseCand1Party_postr==1 
replace votinc_hou_all = 12 if vote_hou==1 & inc_all_hou==1 & HouseCand1Party_postr==1
replace votinc_hou_all = 20 if vote_hou==2 & inc_all_hou==-1 & HouseCand2Party_postr==2
replace votinc_hou_all = 21 if vote_hou==2 & inc_all_hou==0 & HouseCand2Party_postr==2
replace votinc_hou_all = 22 if vote_hou==2 & inc_all_hou==1 & HouseCand2Party_postr==2
*label var votinc_hou_all "Voted for dem/rep by incumbent status - HOUSE"
*label val votinc_hou_all votinc_sen_all
fre votinc_hou_all


***
gen year = 2018

rename caseid caseid18
clonevar caseid = caseid18

recode tookpost 1=0 2=1


****
*create congressional districts
decode state, gen(stated)
drop if stated=="District of Columbia"
encode stated, gen(staten)
label var staten "States from 1 to 50 in alphabetical order"
fre staten // alphabetical order, from 0 to 50

*combine with district
des cdid116_post
rename cdid116_post cd2018str
encode cd2018str, gen(cd2018)
fre cd2018str // problem value 1 instead of 01, 2 instead of 02, etc.
fre cd2018str if staten==2

*turn 1 into 01, 2 into 02 etc.
gen cd2018_first = (substr(cd2018str,1,1))
gen cd2018_second = (substr(cd2018str,2,1))
encode cd2018_first, gen(cd2018_firsten)
encode cd2018_second, gen(cd2018_seconden)
fre cd2018_first if cd2018_seconden==.
gen value0 = 0
egen cd2018_first_correct=concat(value0 cd2018_first) if cd2018_firsten!=. & cd2018_seconden==.
fre cd2018_first cd2018_first_correct if cd2018_seconden==.

gen cd2018str_correct = cd2018_first_correct if cd2018_seconden==.
replace cd2018str_correct = cd2018str if cd2018_seconden!=.
label var cd2018str_correct "Post Congressional district 2018 1=01"
fre cd2018str cd2018str_correct if staten==2
fre cd2018str cd2018str_correct if staten==9
fre cd2018str cd2018str_correct if staten==50

egen stcd=concat(staten cd2018str_correct)

*double check > all correct
fre stcd if staten==9 // florida
fre stcd if staten==9 & cd2018!=.
fre stcd if staten==45 // vermont
fre stcd if staten==45 & cd2018!=.

*
rename stcd stcd_string
gen stcd = real(stcd_string) // turn into numeric
label var stcd_string "State (1 to 50 alphabetical) + congr. district (01 to n) STRING"
label var stcd "State (1 to 50 alphabetical) + congr. district (01 to n)"

***
/*merge with contextual var

save "CCES_2018_recoded2.dta", replace
import excel "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Other files\hr4618.xls", sheet("2018") firstrow clear
sum year stcd inc pwin dv dvp fr po1 po2 redist dexp rexp dpres switchb switcha
drop year switchb switcha
save "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Replication do files\Data\house_data_2018.dta", replace
use "CCES_2018_recoded2.dta", clear
*/

*merge info about previous election
merge m:1 stcd using "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Replication do files\Data\house_data_2016.dta" // all matched from using
drop pwin dv dvp fr redist dexp rexp dpres _merge
rename inc inc_previous
rename po1 po1_previous
rename po2 po2_previous

merge m:1 stcd using "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Replication do files\Data\house_data_2018.dta" // all matched from using
drop _merge

***
*voter registration status post
fre votereg_post
recode votereg_post (2 3=0 "No/DK") (1=1 "Yes"), gen(register_post)
label var register_post "Registered to vote? (post)"
fre register_post

***
*job approval
*Please indicate whether you approve or disapprove of the job that each of the following are doing.
* 1 Strongly approve
* 2 Approve
* 3 Disapprove
* 4 Strongly disapprove
* 5 Never heard of this person

*House representative
fre CC18_311a 
rename CC18_311a approval_house

*senate
*2 senators for each state. 
fre CC18_311b CC18_311c
rename CC18_311b approval_sen1
rename CC18_311c approval_sen2

*question asked also for senators who are not incumbent, like in arizona (3), tennesse (42) and utah (44) in 2018
fre approval_sen1 CurrentSen1Name_post CurrentSen1Party_post if staten==3
fre approval_sen2 CurrentSen2Name_post CurrentSen2Party_post if staten==3

fre approval_sen1 CurrentSen1Name_post CurrentSen1Party_post if staten==42 // problem: question about candidate who dropped out
fre approval_sen2 CurrentSen2Name_post CurrentSen2Party_post if staten==42

fre approval_sen1 CurrentSen1Name_post CurrentSen1Party_post if staten==44
fre approval_sen2 CurrentSen2Name_post CurrentSen2Party_post if staten==44 // problem: question about candidate who dropped out



***
*keep only selected variables
keep gender birthyr educ racer religpewr state state18 weight1 economy ///
pid3 pid7 pid3post pid5post leaners_prof_18 ///
ideo5 ideo7 ///
turnout turnout18 vote_sen votesen18 vote_hou votehou18 ///
SenCand1Incumbent_postr SenCand2Incumbent_postr SenCand1Party_postr SenCand2Party_postr ///
inc_rep_sen inc_all_sen filter_sen filter_sen18 ///
HouseCand1Incumbent_postr HouseCand2Incumbent_postr HouseCand1Party_postr HouseCand2Party_postr ///
inc_rep_hou inc_all_hou filter_hou filter_hou18 ///
votinc_sen_all votinc_hou_all ///
year caseid caseid18 tookpost ///
staten ///
cd2018 cd2018str cd2018str_correct stcd_string stcd ///
inc pwin dv dvp fr po1 po2 redist dexp rexp dpres ///
register_post approval_house approval_sen1 approval_sen2 ///
inc_previous po1_previous po2_previous


*save recoded file
save "CCES_2018_recoded2.dta", replace










